import { _decorator, Component, EventKeyboard, input, Input, Animation, AnimationClip, AudioSource } from 'cc';
import Move from '../../action/move';
import { player_crash } from '../../action/player_crash';
import { dlalog_controller } from '../../action/dialog_controller';
const { ccclass, property } = _decorator;

@ccclass('camera')
export class camera extends Component {
    //声明"对话"按钮属性
    private buttonTalk = null;
    //声明"背包"按钮属性
    private buttonOpenPages = null;
    //声明"资料"按钮属性
    private buttonInfo = null;
    //声明"调查"按钮属性
    private buttonSurvey = null;
    //声明"乘降"按钮属性"
    private buttonGetOn = null;
    //界面按钮鼠标点击后是否可显示
    public static button_click_and_show = true
    //canvas父节点
    private canvas_main = null;
    //canvas的子节点gameNode
    private gameNode = null
    // private gameNode: Node = null;
    //玩家坦克节点
    public static tank_player = null
    //玩家gun节点
    public static tank_player_Gun = null;
    //玩家人物节点
    public static player = null;
    //坦克是否可移动
    public static tank_player_move = true;
    //坦克位置
    public static tank_playerPosition = null
    //相机是否可以跟踪player
    public static camera_follow_player = false
    //是否人类状态
    public static state_human = false
    //player的Animation属性
    private player_Animation = null;
    //player人类状态的面向
    public static state_human_face: string = null;
    //player人类状态面向数据交换
    public static state_human_face_temp: string = null;
    //是否可以从人类状态上车
    public static human_getOn = false

    //音效
    //打开菜单音效
    audio_open_menu = null;
    onLoad() {

    }

    start() {
        console.log("camera start")

        //属性赋值
        this.canvas_main = this.node.getParent();
        camera.tank_player = this.canvas_main.getChildByName("tank_player_1");
        camera.tank_player_Gun = this.canvas_main.getChildByName("tank_player_1_Gun");
        camera.player = this.canvas_main.getChildByName("player_1");
        this.player_Animation = camera.player.getComponent(Animation)

        //按钮属性赋值
        this.buttonTalk = this.node.getChildByName("Button_Talk")
        this.buttonOpenPages = this.node.getChildByName("Button_OpenPages")
        this.buttonInfo = this.node.getChildByName("Button_Info")
        this.buttonSurvey = this.node.getChildByName("Button_Survey")
        this.buttonGetOn = this.node.getChildByName("Button_GetOn")

        //音效赋值,当然我们应该用音频加载的方式来resources那个，不过现在不需要
        this.audio_open_menu = this.getComponent(AudioSource)
        console.log(this.audio_open_menu)

        //注册鼠标"按下"事件
        input.on(Input.EventType.MOUSE_DOWN, this.onMouseDown, this);

        //注册键盘"持续"点击事件
        input.on(Input.EventType.KEY_PRESSING, this.onKeyPressing, this);

        //注册"对话"按钮点击事件
        this.buttonTalk.on(Input.EventType.TOUCH_START, (event) => {

            if (player_crash.allow_dialog) {
                console.log("对话按钮被点击了，开始运行第一次对话")
                dlalog_controller.dialog_01.active = true
            } else {
                console.log("对话按钮被点击了,但是目前不允许对话，因为你的周围没有人...")
            }
        })

        //注册"乘降"按钮点击事件
        this.buttonGetOn.on(Input.EventType.TOUCH_START, (event) => {
            console.log("乘降按钮被点击")
            console.log(camera.human_getOn)
            if (!camera.state_human) {
                //初始化player位置
                camera.player.setPosition(camera.tank_player.getPosition().x - 50, camera.tank_player.getPosition().y);
                //显示player
                camera.player.active = true
                //关闭tank移动,与tank_player_move冲突（菜单控制），所以使用其他属性来控制
                camera.state_human = true
            } else if (camera.state_human && camera.human_getOn) {
                //重新获得节点后，重新指向
                camera.tank_player = this.canvas_main.getChildByName("tank_player_1");
                //重新指向
                camera.player = camera.tank_player;
                //关闭步行状态
                camera.state_human = false
                //重新获得节点，并隐藏步行状态
                camera.player = this.canvas_main.getChildByName("player_1");
                camera.player.active = false
            }
        })

        //初始化坦克位置
        camera.tank_player.setPosition(200, 100);
        //初始化炮口位置
        camera.tank_player_Gun.setPosition(camera.tank_player.getPosition().x, camera.tank_player.getPosition().y)
        //初始化相机位置
        // this.node.setPosition(camera.tank_player.getPosition().x, camera.tank_player.getPosition().y);
        //初始化坦克角度
        camera.tank_player.angle = 270;
        //初始化炮口角度
        camera.tank_player_Gun.angle = 270;
        //player默认隐藏
        camera.player.active = false
        //默认隐藏按钮
        this.buttonTalk.active = false
        this.buttonOpenPages.active = false
        this.buttonInfo.active = false
        this.buttonSurvey.active = false
        this.buttonGetOn.active = false
    }

    update(deltaTime: number) {

    }

    onMouseDown() {
        if (camera.button_click_and_show) {
            if (!this.buttonTalk.active) {
                //停止坦克移动
                camera.tank_player_move = false;

                this.buttonTalk.active = true
                this.buttonOpenPages.active = true
                this.buttonInfo.active = true
                this.buttonSurvey.active = true
                this.buttonGetOn.active = true
            } else {
                //开启坦克移动
                camera.tank_player_move = true;

                this.buttonTalk.active = false
                this.buttonOpenPages.active = false
                this.buttonInfo.active = false
                this.buttonSurvey.active = false
                this.buttonGetOn.active = false
            }
        }
    }

    onKeyPressing(NodeEventType: EventKeyboard): void {
        //每次重新赋值tank_playerPosition
        camera.tank_playerPosition = camera.tank_player.getPosition();

        //坦克移动，菜单栏关闭时，并且需要驾驶状态，tank_player可以移动
        if (camera.tank_player_move && !camera.state_human) {
            //控制player移动，最后一个参数“旋转角度”尽量填5的倍数，因为我设了固定值判断
            Move.tankMove(NodeEventType, camera.tank_player, camera.tank_playerPosition, 5, 10);
            Move.tankMove(NodeEventType, camera.tank_player_Gun, camera.tank_playerPosition, 5, 5);
            //炮口跟踪tank_player
            camera.tank_player_Gun.setPosition(camera.tank_player.getPosition().x, camera.tank_player.getPosition().y)
            if ((camera.tank_playerPosition.x <= 10 && camera.tank_playerPosition.x >= -10) && (camera.tank_playerPosition.y <= 10 && camera.tank_playerPosition.y >= -10)) {
                //开启相机跟踪player
                camera.camera_follow_player = true;
            }
            // console.log(camera.tank_player.position)
        }

        //人类状态移动，菜单栏关闭，并且人类状态，player才可以移动
        if (camera.tank_player_move && camera.state_human) {
            //因为player已经重新指向，所以我们要重新赋值
            camera.player = this.canvas_main.getChildByName("player_1");
            this.player_Animation = camera.player.getComponent(Animation)

            //转向并且播放动画
            //向同一个方向时不调用play方法
            if (this.animationPlay(camera.state_human_face_temp)) {
                this.player_Animation.play(camera.state_human_face)
            }

            //人类移动
            Move.humanMove(NodeEventType, camera.player, camera.player.getPosition(), 5);
        }
        if (camera.camera_follow_player) {
            this.node.setPosition(camera.tank_playerPosition.x, camera.tank_playerPosition.y);
        }
    }

    /**
     * 是否执行animation.play()，为了解决play重复调用问题，同一个面向时不调用play方法
     * @param state humanMove方法传来的参数
     * @returns 
     */
    animationPlay(state): boolean {
        //如果传入的参数与目前不同
        if (camera.state_human_face != state) {
            camera.state_human_face = state
            //执行play
            return true
        }
        //如果传入的参数和目前相同
        if (camera.state_human_face == state) {
            camera.state_human_face = state
            //不执行play
            return false
        }
    }
}

